package cn.com.thingo.hyintelligent.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hikvision.wifi.configuration.BaseUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.base.BaseActivity;
import cn.com.thingo.hyintelligent.base.contract.EzWifiNetConfigContract;
import cn.com.thingo.hyintelligent.presenter.EzWifiNetConfigPresenter;

public class EzWifiNetConfigActivity extends BaseActivity<EzWifiNetConfigPresenter> implements EzWifiNetConfigContract.View, View.OnClickListener {

    /**
     * wifi密码
     */
    public static final String WIFI_PASSWORD = "wifi_password";

    /**
     * wifiSSID
     */
    public static final String WIFI_SSID = "wifi_ssid";

    /**
     * deviceType
     */
    public static final String DEVICE_TYPE = "device_type";

    public static final String SUPPORT_WIFI = "support_Wifi";
    public static final String SUPPORT_NET_WORK = "support_net_work";
    public static String IS_FROM_DEVICE_SETTING = "FromPage";
    @BindView(R.id.toolbar_left)
    ImageView ivLeft;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    @BindView(R.id.btnNext)
    Button btnNext;
    @BindView(R.id.tvSSID)
    TextView tvSSID;
    @BindView(R.id.edtPassword)
    EditText edtPassword;

    private String seriaNo;

    private String veryCode = null;

    private boolean isSupportNetWork = false;

    private boolean isFromDeviceSetting;

    private String deviceType;

    protected static final String CONFIG_TYPE_KEY = "config_type";
    protected static final int CONFIG_TYPE_WIRED_CONNECTION = 0;
    protected static final int CONFIG_TYPE_SMARTCONFIG = 1;
    protected static final int CONFIG_TYPE_SOUNDWAVE = 2;
    protected static final int CONFIG_TYPE_AP = 3;
    protected static final int CONFIG_TYPE_SMART_SOUNDWAVE = 4;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_ezwifi_net_config;
    }

    @Override
    protected void initEventAndData() throws Exception {
        init();
        initTitleBar();
        initUI();
        setListener();
    }

    private void init() {
        seriaNo = getIntent().getStringExtra(SearchEzDeviceActivity.BUNDE_SERIANO);
        veryCode = getIntent().getStringExtra(SearchEzDeviceActivity.BUNDE_VERYCODE);
        isSupportNetWork = getIntent().getBooleanExtra(SUPPORT_NET_WORK, false);
        isFromDeviceSetting = getIntent().getBooleanExtra(IS_FROM_DEVICE_SETTING, false);
        deviceType = getIntent().getStringExtra(DEVICE_TYPE);
    }

    private void initTitleBar() {
        ivLeft.setImageResource(R.mipmap.back);
        tvTitle.setText(R.string.auto_wifi_cer_config_title1);
    }

    private void initUI() {
        if (isFromDeviceSetting) {
            tvTitle.setText(R.string.auto_wifi_cer_config_title1);
        } else {
            tvTitle.setText(R.string.auto_wifi_cer_config_title2);
        }
        tvSSID.setText(BaseUtil.getWifiSSID(this));

        WifiManager mWifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        // 取得WifiInfo对象
        WifiInfo mWifiInfo = mWifiManager.getConnectionInfo();
        // 路由器的mac地址
        String mac = (mWifiInfo == null) ? "" : mWifiInfo.getBSSID();
        String password = "";
//        if (!TextUtils.isEmpty(mac)) {
//            password = LocalInfo.getInstance().getWifiPassword(mac);
//        }

        edtPassword.setText(password);
    }

    private void setListener() {
        btnNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.btnNext:
                //if (isFromDeviceSetting) {
                //    //intent = new Intent(this, AutoWifiOfflineDeviceActivity.class);
                //} else {
                //    intent = new Intent(this, AutoWifiConnectingActivity.class);
                //}
                //intent.putExtra(WIFI_SSID, tvSSID.getText().toString());
                //intent.putExtra(WIFI_PASSWORD, TextUtils.isEmpty(edtPassword.getText().toString()) ? "smile"
                //        : edtPassword.getText().toString());
                //intent.putExtra(SeriesNumSearchActivity.BUNDE_SERIANO, seriaNo);
                //intent.putExtra(SeriesNumSearchActivity.BUNDE_VERYCODE, veryCode);
                //intent.putExtra(SUPPORT_WIFI, true);
                //intent.putExtra(SUPPORT_NET_WORK, isSupportNetWork);
                //intent.putExtra(ResetIntroduceActivity.IS_FROM_DEVICE_SETTING, isFromDeviceSetting);
                //intent.putExtra(DEVICE_TYPE, deviceType);
                //startActivity(intent);
                showSelectNetConfig();
                break;
            default:
                break;
        }
    }

    private void showSelectNetConfig() {
        final List<String> list = new ArrayList<String>();
        final List<Integer> list_types = new ArrayList<Integer>();
        list.add(getString(R.string.string_wired_connection));
        list_types.add(CONFIG_TYPE_WIRED_CONNECTION);

        boolean support_Wifi = getIntent().getBooleanExtra("support_Wifi", false);
        boolean support_Ap = getIntent().getBooleanExtra("support_Ap", false);
        boolean support_sound_wave = getIntent().getBooleanExtra("support_sound_wave", false);
        boolean support_unknow_mode = getIntent().getBooleanExtra("support_unknow_mode", false);
        if (support_unknow_mode) {
            // 未查询到设备信息，不确定设备支持的配网能力,需要用户根据指示灯判断
            //若设备指示灯红蓝闪烁，请选择
            list.add(getString(R.string.string_smart_connection_normal));
            list_types.add(CONFIG_TYPE_SMARTCONFIG);

            // 若设备指示灯蓝色闪烁，请选择
            list.add(getString(R.string.string_ap_connection_normal));
            list_types.add(CONFIG_TYPE_AP);

        } else {
            // 查询到设备信息，根据能力级选择配网方式
            if (support_Wifi) {
                list.add(getString(R.string.string_smart_connection));
                list_types.add(CONFIG_TYPE_SMARTCONFIG);
            }
            if (support_sound_wave) {
                list.add(getString(R.string.string_sound_wave_connection));
                list_types.add(CONFIG_TYPE_SOUNDWAVE);
            }
            if (support_Ap) {
                list.add(getString(R.string.string_ap_connection));
                list_types.add(CONFIG_TYPE_AP);
            }
        }

        final String items[] = new String[list.size()];
        list.toArray(items);
        AlertDialog.Builder builder = new AlertDialog.Builder(this,AlertDialog.THEME_HOLO_LIGHT);
        builder.setTitle(R.string.string_device_network_mode).setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, final int i) {
                dialogInterface.dismiss();
                Intent intent = null;
                switch (list_types.get(i)) {
                    case CONFIG_TYPE_WIRED_CONNECTION:
                        // TODO: 2018/6/28 有线连接
                        break;
                    case CONFIG_TYPE_SMARTCONFIG:
                        // TODO: 2018/6/26 smart config wifi
                        intent = new Intent(EzWifiNetConfigActivity.this,EzWifiConnectionActivity.class);

                        intent.putExtra(WIFI_SSID, tvSSID.getText().toString());
                        intent.putExtra(WIFI_PASSWORD, TextUtils.isEmpty(edtPassword.getText().toString()) ? "smile"
                                : edtPassword.getText().toString());
                        intent.putExtra(SearchEzDeviceActivity.BUNDE_SERIANO, seriaNo);
                        intent.putExtra(SearchEzDeviceActivity.BUNDE_VERYCODE, veryCode);
                        intent.putExtra("support_Wifi", true);
                        intent.putExtra(SUPPORT_NET_WORK, isSupportNetWork);
                        intent.putExtra(IS_FROM_DEVICE_SETTING, isFromDeviceSetting);
                        intent.putExtra(DEVICE_TYPE, deviceType);
                        startActivity(intent);
                        break;
                    case CONFIG_TYPE_SOUNDWAVE:
                        // TODO: 2018/6/26 声波配网
                        intent = new Intent(EzWifiNetConfigActivity.this, EzWifiConnectionActivity.class);
                        intent.putExtra(WIFI_SSID, tvSSID.getText().toString());
                        intent.putExtra(WIFI_PASSWORD, TextUtils.isEmpty(edtPassword.getText().toString()) ? "smile"
                                : edtPassword.getText().toString());
                        intent.putExtra(SearchEzDeviceActivity.BUNDE_SERIANO, seriaNo);
                        intent.putExtra(SearchEzDeviceActivity.BUNDE_VERYCODE, veryCode);
                        intent.putExtra("support_sound_wave", true);
                        intent.putExtra(SUPPORT_NET_WORK, isSupportNetWork);
                        intent.putExtra(IS_FROM_DEVICE_SETTING, isFromDeviceSetting);
                        intent.putExtra(DEVICE_TYPE, deviceType);
                        startActivity(intent);
                        break;
                    case CONFIG_TYPE_AP:
                        // TODO: 2018/6/26 ap模式配网
                        intent = new Intent(EzWifiNetConfigActivity.this, EzWifiApConfigActivity.class);
                        intent.putExtra(WIFI_SSID, tvSSID.getText().toString());
                        intent.putExtra(WIFI_PASSWORD, TextUtils.isEmpty(edtPassword.getText().toString()) ? "smile"
                                : edtPassword.getText().toString());
                        intent.putExtra(SearchEzDeviceActivity.BUNDE_SERIANO, seriaNo);
                        intent.putExtra(SearchEzDeviceActivity.BUNDE_VERYCODE, veryCode);
                        //intent.putExtras(getIntent());
                        startActivity(intent);
                        break;
                    case CONFIG_TYPE_SMART_SOUNDWAVE:
                        // TODO: 2018/6/26 声波配网+普通配网同时进行
                        intent = new Intent(EzWifiNetConfigActivity.this, EzWifiConnectionActivity.class);
                        intent.putExtra(WIFI_SSID, tvSSID.getText().toString());
                        intent.putExtra(WIFI_PASSWORD, TextUtils.isEmpty(edtPassword.getText().toString()) ? "smile"
                                : edtPassword.getText().toString());
                        intent.putExtra(SearchEzDeviceActivity.BUNDE_SERIANO, seriaNo);
                        intent.putExtra(SearchEzDeviceActivity.BUNDE_VERYCODE, veryCode);
                        intent.putExtra("support_sound_wave", true);
                        intent.putExtra("support_Wifi", true);
                        intent.putExtra(SUPPORT_NET_WORK, isSupportNetWork);
                        intent.putExtra(IS_FROM_DEVICE_SETTING, isFromDeviceSetting);
                        intent.putExtra(DEVICE_TYPE, deviceType);
                        startActivity(intent);
                        break;
                    default:
                        break;
                }
            }
        }).setNegativeButton(android.R.string.cancel, null).show();
    }


    @OnClick({R.id.toolbar_left})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
        }
    }
}
